//
//  ZHFAllViewController.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFAllViewController.h"

#import "ZHFCollectionViewCell.h"

#import "ZHFDestination.h"

#import "ZHFAreaViewController.h"

#import "ZHFData.h"

@interface ZHFAllViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *mainCollection;


@end

@implementation ZHFAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = [[self.array objectAtIndex:self.index] title];
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_button"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 32 * ADAPT_WIDTH) / 2, (SCREEN_WIDTH - 32 * ADAPT_WIDTH) / 2);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10 * ADAPT_WIDTH, 10, 10 * ADAPT_WIDTH);
    
    self.mainCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 60) collectionViewLayout:flowLayout];
    self.mainCollection.delegate = self;
    self.mainCollection.dataSource = self;
    self.mainCollection.backgroundColor = self.view.backgroundColor;
    [self.mainCollection registerClass:[ZHFCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    [self.view addSubview:self.mainCollection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [[self.array objectAtIndex:self.index] array].count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHFCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = self.view.backgroundColor;
    cell.data = [[[self.array objectAtIndex:self.index] array] objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark item 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHFAreaViewController *areaVC = [[ZHFAreaViewController alloc] init];
    
    ZHFData *data = [[[self.array objectAtIndex:self.index] array] objectAtIndex:indexPath.row];
    areaVC.Id = [data Id];
    areaVC.navigationItem.title = [NSString stringWithFormat:@"%@攻略", [data name_zh_cn]];
    [self.navigationController pushViewController:areaVC animated:YES];
}

#pragma mark - 返回
- (void)leftAction:(UIBarButtonItem *)bar {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
